//:  v.book.read.tip.observer.PaySuccess.java
package v.book.read.tip.observer;

import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author 小败
 */
public class PaySuccess {
    
    private boolean success;
    private SuccessNotifier sNotifier = new SuccessNotifier();
    
    public PaySuccess() {
        this.success = false;
    }
    
    public void success() {
        System.out.println("update database: order is successed!");
        this.success = true;
        sNotifier.notifyObservers();
    }
    
    public SuccessNotifier successed() {
        return sNotifier;
    }
    
    private class SuccessNotifier extends Observable {
        
        // query to database.
        private boolean alreadySuccess = false;
        
        @Override
        public void notifyObservers() {
            if (success && !alreadySuccess) {
                setChanged();
                super.notifyObservers();
                this.alreadySuccess = true;
            }
        }
        
    }
    
    public static void main(String[] args) {
        PaySuccess paySuccess = new PaySuccess();
        paySuccess.successed().addObserver(new PointObserver());
        paySuccess.successed().addObserver(new ActivityObserver());
        paySuccess.success();
    }

}


class PointObserver implements Observer {

    public void update(Observable o, Object arg) {
        System.out.println("update point: order is successed!");
    }
    
}

class ActivityObserver implements Observer {

    public void update(Observable o, Object arg) {
        System.out.println("update activity: order is successed!");
    }
    
}
